package oj.swing.event;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import oj.swing.cliente.OJSwingFrameLoader;
import oj.swing.common.VerticalFlowLayout;
import oj.swing.component.OJSwingSearchTable;
import oj.swing.component.OJSwingTabCont;
import oj.swing.component.OJSwingToolBar;
import oj.swing.component.OjSwingPanel;
import oj.util.common.OJEntry;

import com.intelap.iaswing.IASwingScrollPane;
import com.intelap.iaswing.IASwingTable;

public class OJSwingFrameListener implements ListSelectionListener,
		ActionListener {

	private OJSwingFrameLoader loader;

	private OJSwingToolBar bar;

	private int index;

	private OJEntry entr;

	public OJSwingFrameListener(OJSwingFrameLoader loader) {

		this.loader = loader;
		this.index = -1;

	}

	public void valueChanged(ListSelectionEvent e) {
		try {

			if (e.getValueIsAdjusting())
				return;

			JList theList = (JList) e.getSource();
			if (theList.isSelectionEmpty()) {
				return;
			} else {

				index = theList.getSelectedIndex();

				entr = (OJEntry) loader.cache.get(BigDecimal.valueOf(index));
				if (entr != null) {

					/** Habilito el boton de Insert */
					getBar().getInsert().setEnabled(true);

					if (entr.getClase().trim().equals("")) {

						/*
						 * JPanel panelPrueba = new JPanel();
						 * panelPrueba.setBounds(0, 0, 400, 400);
						 * panelPrueba.setLayout(new VerticalFlowLayout()); //
						 * splitPanel.setRightComponent(new JPanel()); // Busco
						 * los componentes - Prueba
						 * 
						 * ArrayList componentes =
						 * loader.remote.ojComponentes(); System.out
						 * .println("Componenetes " + componentes.size());
						 * 
						 * OJSwingTabCont count = new OJSwingTabCont();
						 * count.builTab(componentes); // for (int a = 0; a <
						 * componentes.size(); a++) { // //
						 * System.out.println("Dentro del for"); //
						 * DataSTableField data = (DataSTableField) componentes //
						 * .get(a); // // OJSwingComponent c = new
						 * OJSwingComponent(); // c.setLabelSize(12); //
						 * c.setLabelLayout(false); //
						 * c.setLabel(data.getDESCRIPTION()); //
						 * c.setType(data.getVISUAL_TYPE().charAt(0)); //
						 * c.validate(); // panelPrueba.add(c); // // } // //
						 * panelPrueba.setBorder(BorderFactory.createLineBorder( //
						 * Color.blue, 2)); // panelPrueba.validate(); //
						 * panelPrueba.setVisible(true); //
						 * splitPanel.setRightComponent(panelPrueba);
						 * loader.splitPanel.setRightComponent(count);
						 */

					} else {

						/** crea una clase por Reflexion */
						try {
							/** Busco si no esta instanciado */
							if (!loader.cachePaneles.containsKey(BigDecimal
									.valueOf(index))) {

								/**
								 * ClassLoader laoader = ClassLoader
								 * .getSystemClassLoader(); Class c =
								 * laoader.loadClass(entr.getClase());
								 */

								// JPanel panels = (JPanel) c.getConstructor(
								// new Class[] { String.class })
								// .newInstance(new Object[] { " " });
								JPanel panels = new JPanel();
								panels.setLayout(new VerticalFlowLayout());

								/** IASwingTable Tablas */

								OJSwingSearchTable searchTable = new OJSwingSearchTable(
										entr.getIdSearch());

								IASwingTable tb = searchTable.getTable();

								IASwingScrollPane scrollPane = new IASwingScrollPane();
								scrollPane = new IASwingScrollPane();

								scrollPane.getViewport().add(tb);
								scrollPane.setEnabled(true);
								tb.setVisible(true);
								// panels.setBounds(0, 0, 400, 400);

								panels.add(scrollPane);
								panels.setVisible(true);
								panels.repaint();
								panels.validate();

								loader.splitPanel.setRightComponent(panels);
								loader.setMenuActive(panels);
								loader.cachePaneles.put(BigDecimal
										.valueOf(index), panels);
							} else {
								loader.splitPanel
										.setRightComponent((JPanel) loader.cachePaneles
												.get(BigDecimal.valueOf(index)));
							}
						}

						catch (ClassNotFoundException ex) {
							JOptionPane.showMessageDialog(loader,
									"No se encontro la clase");
							loader.splitPanel.setRightComponent(new JPanel());
							loader.splitPanel.repaint();
							loader.setMenuActive(null);

						}

					}
				}
				loader.splitPanel.repaint();

			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	/** Manejo del Insert de la Barra de menu */
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("insert")) {

			JPanel panelPrueba = new JPanel();
			panelPrueba.setBounds(0, 0, 400, 400);
			panelPrueba.setLayout(new VerticalFlowLayout());

			// splitPanel.setRightComponent(new JPanel());
			/** Busco los componentes - Prueba */

			ArrayList componentes;

			try {
				componentes = loader.remote.ojComponentes();

				ClassLoader laoader = ClassLoader.getSystemClassLoader();

				Class c = laoader.loadClass(entr.getClase());

				OjSwingPanel validation = (OjSwingPanel) c.getConstructor(
						new Class[] { String.class }).newInstance(
						new Object[] { " " });

				OJSwingTabCont count = new OJSwingTabCont(validation);				
				count.builTab(componentes);
				validation.setDataset(count.getDataset());

				// for (int a = 0; a < componentes.size(); a++) {
				//
				// System.out.println("Dentro del for");
				// DataSTableField data = (DataSTableField) componentes
				// .get(a);
				//
				// OJSwingComponent c = new OJSwingComponent();
				// c.setLabelSize(12);
				// c.setLabelLayout(false);
				// c.setLabel(data.getDESCRIPTION());
				// c.setType(data.getVISUAL_TYPE().charAt(0));
				// c.validate();
				// panelPrueba.add(c);
				//
				// }
				//
				// panelPrueba.setBorder(BorderFactory.createLineBorder(
				// Color.blue, 2));
				// panelPrueba.validate();
				// panelPrueba.setVisible(true);

				// splitPanel.setRightComponent(panelPrueba);
				loader.splitPanel.setRightComponent(count);

				// JPanel panels = (JPanel) c.getConstructor(
				// new Class[] { String.class })
				// .newInstance(new Object[] { " " });

			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
		{
			if (e.getActionCommand().equals("browser")) {

				loader.splitPanel
						.setRightComponent((JPanel) loader.cachePaneles
								.get(BigDecimal.valueOf(index)));
				loader.splitPanel.repaint();
			}

		}

	}

	public OJSwingToolBar getBar() {
		return bar;
	}

	public void setBar(OJSwingToolBar bar) {
		this.bar = bar;
	}
}
